//
//  BSTopMenuView.m
//  Baisijie
//
//  Created by 王建伟 on 2017/3/13.
//  Copyright © 2017年 RangGuangyu. All rights reserved.
//

#import "BSTopMenuView.h"
#import "BSTopMenuItem.h"
#import "BSTopMenuButton.h"

@interface BSTopMenuView ()

/** 滚动视图 */
@property (nonatomic ,weak) UIScrollView *scrollView;
/** 存放的按钮数组 */
@property (nonatomic ,strong) NSMutableArray *btns;
/** 底部滑块 */
@property (nonatomic ,weak) UILabel *bottomView;
/** 选中的按钮 */
@property (nonatomic ,weak) BSTopMenuButton *selectedBtn;

@end

@implementation BSTopMenuView

- (UILabel *)bottomView {
    if (!_bottomView) {
        UILabel *bottomView = [[UILabel alloc] init];
        bottomView.bs_y = self.scrollView.bs_height - 3;
        bottomView.bs_height = 2;
        BSTopMenuButton *lastBtn = _btns.lastObject;
        bottomView.backgroundColor = [lastBtn titleColorForState:UIControlStateSelected];
        [self.scrollView addSubview:bottomView];
        _bottomView = bottomView;
    }
    return _bottomView;
}
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"startBar_bg"]];
        [self setupScrollView];
    }
    return self;
}
- (void)setupScrollView {
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = CGRectMake(0, BSStatusBarHeight, KScreenW, self.bs_height - BSStatusBarHeight);
    scrollView.backgroundColor = [UIColor clearColor];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.contentInset = UIEdgeInsetsMake(0, BSMargin, 0, BSMargin);
    [self addSubview:scrollView];
    _scrollView = scrollView;
    
    UIImageView *leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"left_bg"]];
    leftView.bs_x = 0;
    leftView.bs_y = BSStatusBarHeight;
    [self addSubview:leftView];
    
    UIImageView *fightView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right_bg"]];
    fightView.bs_x = self.bs_width - fightView.bs_width;
    fightView.bs_y = BSStatusBarHeight;
    [self addSubview:fightView];
}
- (void)setDataSource:(NSMutableArray *)dataSource {
    _dataSource = dataSource;
    _btns = [NSMutableArray array];
    for (int i = 0; i < dataSource.count; i ++) {
        //取出最后一个按钮
        BSTopMenuButton *lastBtn = _btns.lastObject;
        BSTopMenuItem *item = dataSource[i];
        BSTopMenuButton *btn = [BSTopMenuButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i + 1;
        [btn setTitle:item.name forState:UIControlStateNormal];
        [btn sizeToFit];
        btn.bs_width += 20;
        btn.bs_x = CGRectGetMaxX(lastBtn.frame);
        btn.bs_y = 0;
        btn.bs_height = _scrollView.bs_height;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_scrollView addSubview:btn];
        [_btns addObject:btn];
    }
    //取出第一个按钮
    BSTopMenuButton *firstBtn = _btns.firstObject;
    [self btnClick:firstBtn];
    //取出最后一个按钮
    BSTopMenuButton *lastBtn = _btns.lastObject;
    _scrollView.contentSize = CGSizeMake(CGRectGetMaxX(lastBtn.frame), 0);
}
- (void)btnClick:(BSTopMenuButton *)btn {
    if (_selectedBtn == btn) {
        [BSNotificationCenter postNotificationName:BSTopicButtonDidRepeatClickNotification object:nil];
    }
    _selectedBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    _selectedBtn.selected = NO;
    btn.selected = YES;
    btn.titleLabel.font = [UIFont systemFontOfSize:17];
    _selectedBtn = btn;
    [UIView animateWithDuration:0.25 animations:^{
        self.bottomView.bs_width = btn.bs_width - 15;
        self.bottomView.bs_centerX = btn.bs_centerX;
    }];
    if (self.topicBtnClickBlock) {
        self.topicBtnClickBlock(btn.tag - 1);
    }
    CGFloat offsetX = btn.bs_centerX - self.bs_width * 0.5;
    if (offsetX < 0) {
        offsetX = -_scrollView.contentInset.left;
    } else if (btn.bs_centerX + self.bs_width * 0.5 > _scrollView.contentSize.width) {
        offsetX = _scrollView.contentSize.width - self.bs_width + _scrollView.contentInset.right;
    }
    [_scrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}
- (void)setSelectedIndex:(NSInteger)selectedIndex {
//    _selectedIndex = selectedIndex;
    BSTopMenuButton *selectedBtn = [_scrollView viewWithTag:selectedIndex + 1];
    [self btnClick:selectedBtn];
}
- (NSInteger)selectedIndex {
    return _selectedBtn.tag - 1;
}


@end
